package com.onenine.app.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatDialog;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.onenine.app.R;

/**
 * 动态封面
 */
public class DynamicCoverDialog extends AppCompatDialog {

    private TextView mTvMessage;
    private String mMessage;

    public interface OnItemClickListener {
        void onItemClick(Dialog dialog, String message);
    }

    public DynamicCoverDialog(Context context, String message) {
        super(context, R.style.DialogStyle);
        mMessage = message;
        initView(context);
    }

    private void initView(Context context) {
        setCancelable(false);
        setCanceledOnTouchOutside(true);
        View view = LayoutInflater.from(context)
                .inflate(R.layout.layout_dynamic_cover_dialog, null);
        mTvMessage = view.findViewById(R.id.tv_msg);
        mTvMessage.setText(mMessage);
        setContentView(view);
        getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT
                , ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    public DynamicCoverDialog setOnItemClickListener(final OnItemClickListener onItemClickListener) {
        mTvMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                onItemClickListener.onItemClick(DynamicCoverDialog.this, mMessage);
            }
        });
        return this;
    }

    @Override
    public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            dismiss();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
